package com.teacher.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teacher.auto.JSONAuthentication;
import com.teacher.entity.CollegeMajor;
import com.teacher.entity.TUser;
import com.teacher.mapper.CollegeMajorMapper;
import com.teacher.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author teacher
 * @since 2022-04-09
 */
@RestController
@RequestMapping("/college-major")
public class CollegeMajorController extends JSONAuthentication {
    @Autowired
    CollegeMajorMapper collegeMajorMapper;
    @ApiOperation(value = "添加专业")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody CollegeMajor collegeMajor) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("major",collegeMajor.getMajor());
        CollegeMajor collegeMajor1 = collegeMajorMapper.selectOne(wrapper);
        if(collegeMajor1!=null){
            Result error = Result.error().message("该专业已存在");
            this.WriteJSON(request, response, error);
        }else{
            if(collegeMajorMapper.insert(collegeMajor)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }

    }
    @ApiOperation(value = "删除专业")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = collegeMajorMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据id修改专业所属学院")
    @PostMapping("/updCollegeById")
    public void updCollegeById(HttpServletRequest request, HttpServletResponse response, @RequestBody CollegeMajor collegeMajor) throws Exception {
        CollegeMajor collegeMajor1 = collegeMajorMapper.selectById(collegeMajor.getId());
        collegeMajor1.setCollege(collegeMajor.getCollege());
        if(collegeMajorMapper.updateById(collegeMajor1)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }


    @ApiOperation(value = "查找所有学院专业")
    @PostMapping("/findAllUser")
    public void findById(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        Page<CollegeMajor> shUserPage = new Page<>(current, size);
        Page<CollegeMajor> shUserPages = collegeMajorMapper.selectPage(shUserPage, null);
        List<CollegeMajor> records = shUserPages.getRecords();

        int total = collegeMajorMapper.selectCount(null);
        if (records.size() > 0) {
            Result ok = Result.ok().data("CollegeMajor", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找学院专业根据学院名字")
    @PostMapping("/findAllUserByCollegeName")
    public void findAllUserByCollegeName(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("collName") String name) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("college",name);
        Page<CollegeMajor> shUserPage = new Page<>(current, size);
        Page<CollegeMajor> shUserPages = collegeMajorMapper.selectPage(shUserPage, wrapper);
        List<CollegeMajor> records = shUserPages.getRecords();
        int total = collegeMajorMapper.selectCount(wrapper);
        if (records.size() > 0) {
            Result ok = Result.ok().data("CollegeMajor", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找学院名字根据专业名")
    @PostMapping("/findByMajorName")
    public void findByMajorName(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("major") String major) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("major",major);
        Page<CollegeMajor> shUserPage = new Page<>(current, size);
        Page<CollegeMajor> shUserPages = collegeMajorMapper.selectPage(shUserPage, wrapper);
        List<CollegeMajor> records = shUserPages.getRecords();
        int total = collegeMajorMapper.selectCount(wrapper);
        if (records.size() > 0) {
            Result ok = Result.ok().data("CollegeMajor", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

